from flask import Flask, jsonify
from flask_cors import CORS
import datetime

app = Flask(__name__)
CORS(app)  # 允许跨域请求


# 模拟图表数据
@app.route('/api/charts/line')
def line_chart():
    return jsonify({
        "xAxis": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        "seriesName": "销售额",
        "seriesData": [120, 190, 150, 210, 180, 250, 170]
    })


@app.route('/api/charts/bar')
def bar_chart():
    return jsonify({
        "xAxis": ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        "seriesName": "销量",
        "seriesData": [5, 20, 36, 10, 10, 20]
    })


@app.route('/api/charts/pie')
def pie_chart():
    return jsonify({
        "seriesName": "访问来源",
        "legendData": ["搜索引擎", "直接访问", "外部链接", "社交媒体"],
        "seriesData": [
            {"value": 335, "name": "搜索引擎"},
            {"value": 310, "name": "直接访问"},
            {"value": 234, "name": "外部链接"},
            {"value": 135, "name": "社交媒体"}
        ]
    })


@app.route('/api/charts/scatter')
def scatter_chart():
    return jsonify({
        "seriesData": [
            [10.0, 8.04], [8.0, 6.95], [13.0, 7.58], [9.0, 8.81],
            [11.0, 8.33], [14.0, 9.96], [6.0, 7.24], [4.0, 4.26],
            [12.0, 10.84], [7.0, 4.82], [5.0, 5.68]
        ]
    })


@app.route('/api/charts/radar')
def radar_chart():
    return jsonify({
        "legendData": ["预算分配", "实际支出"],
        "indicators": [
            {"name": "销售", "max": 6500},
            {"name": "管理", "max": 16000},
            {"name": "信息技术", "max": 30000},
            {"name": "客户支持", "max": 38000},
            {"name": "研发", "max": 52000},
            {"name": "市场", "max": 25000}
        ],
        "seriesData": [
            {"value": [4200, 3000, 20000, 35000, 50000, 18000], "name": "预算分配"},
            {"value": [5000, 14000, 28000, 26000, 42000, 21000], "name": "实际支出"}
        ]
    })


@app.route('/api/charts/gauge')
def gauge_chart():
    return jsonify({
        "value": 75,
        "name": "完成率"
    })


@app.route('/api/charts/funnel')
def funnel_chart():
    return jsonify({
        "legendData": ["展示", "点击", "访问", "咨询", "订单"],
        "seriesName": "转化漏斗",
        "seriesData": [
            {"value": 60, "name": "访问"},
            {"value": 40, "name": "咨询"},
            {"value": 20, "name": "订单"},
            {"value": 80, "name": "点击"},
            {"value": 100, "name": "展示"}
        ]
    })


@app.route('/api/charts/heatmap')
def heatmap_chart():
    return jsonify({
        "xAxis": ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        "yAxis": ["上午", "下午", "晚上"],
        "minValue": 0,
        "maxValue": 10,
        "seriesData": [
            [0, 0, 5], [1, 0, 7], [2, 0, 3], [3, 0, 5], [4, 0, 8], [5, 0, 4], [6, 0, 6],
            [0, 1, 7], [1, 1, 6], [2, 1, 9], [3, 1, 4], [4, 1, 7], [5, 1, 3], [6, 1, 2],
            [0, 2, 1], [1, 2, 2], [2, 2, 3], [3, 2, 1], [4, 2, 5], [5, 2, 8], [6, 2, 2]
        ]
    })


@app.route('/api/charts/theme-river')
def theme_river_chart():
    # 生成主题河流图的时间序列数据
    today = datetime.datetime.now()
    data = []
    themes = ["主题1", "主题2", "主题3", "主题4", "主题5", "主题6"]

    for day in range(30):  # 30天数据
        date = (today - datetime.timedelta(days=day)).strftime('%Y-%m-%d')
        for theme in themes:
            value = int(10 + 90 * (1 - day / 30) * (0.5 + 0.5 * (ord(theme[1]) % 10) / 10))
            data.append([date, value, theme])

    return jsonify({
        "legendData": themes,
        "seriesData": data
    })


if __name__ == '__main__':
    app.run(debug=True, port=5000)